package com.makerworks.medu;

import android.app.Activity;
import android.content.Intent;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaArgs;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONException;

/**
 * Created by xuexin on 2016/10/18.
 */

public class BluetoothConnectHelper extends CordovaPlugin {
    private static CallbackContext startListenConnected;
    private static CallbackContext startListenDisconnected;
    private static CallbackContext startListenReceivedData;
    private static CallbackContext startListenBackButtonClicked;

    public static void onReceiveData(byte[] data) {
        if (startListenReceivedData != null) {
            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, data);
            pluginResult.setKeepCallback(true);
            startListenReceivedData.sendPluginResult(pluginResult);
        }
    }

    public static void onConnencted() {
        if (startListenConnected != null) {
            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, "已连接");
            pluginResult.setKeepCallback(true);
            startListenConnected.sendPluginResult(pluginResult);
        }
    }

    public static void onDisconnected() {
        if (startListenDisconnected != null) {
            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, "断开连接");
            pluginResult.setKeepCallback(true);
            startListenDisconnected.sendPluginResult(pluginResult);
        }
    }

    public static void onPressBack() {
        if (startListenBackButtonClicked != null) {
            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, "用户返回");
            pluginResult.setKeepCallback(true);
            startListenBackButtonClicked.sendPluginResult(pluginResult);
        }
    }

    @Override
    public boolean execute(String action, CordovaArgs args, CallbackContext callbackContext) throws JSONException {
        if (action.equals("showBluetoothConnectScence")) {
            //先断连
            if (ConnectActivity.Obj.INSTANCE.getHasShow())
                return true;
            ConnectActivity.Obj.INSTANCE.setHasShow(true);
            BluetoothAdapter_BLE.getInstance().disconnect();
            Activity activity = cordova.getActivity();
            activity.startActivity(new Intent(activity, ConnectActivity.class));
            return true;
        } else if (action.equals("dismissBluetoothConnectScence")) {
            Activity activity = ConnectActivity.Obj.INSTANCE.getInstance();
            if (activity != null) {
                activity.finish();
            }
            return true;
        } else if (action.equals("sendData")) {
            byte[] data = args.getArrayBuffer(0);
            BluetoothAdapter_BLE.getInstance().write(data);
            return true;
        } else if (action.equals("startListenConnected")) {
            startListenConnected = callbackContext;
            return true;
        } else if (action.equals("startListenDisconnected")) {
            startListenDisconnected = callbackContext;
            return true;
        } else if (action.equals("startListenReceivedData")) {
            startListenReceivedData = callbackContext;
            return true;
        } else if (action.equals("disConnect")) {
            BluetoothAdapter_BLE.getInstance().disconnect();
            return true;
        } else if (action.equals("startListenBackButtonClicked")) {
            startListenBackButtonClicked = callbackContext;
            return true;
        }
        return false;
    }
}
